<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/user_dashboard.css">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_1309180_m0vigzfu7y.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <title>我的界面</title>
    <style>
        body {
            background-image: url('https://www.beihaiting.com/uploads/allimg/150528/10723-15052QAI0201.jpg'); 
            background-size: cover;
            background-attachment: fixed;
            color: #fff;
        }

        .header {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .header h1 {
            margin: 0;
            color: #020202;
        }

        .follow-list,
        .complaint-section {
            width: 80%;
            margin: 0 auto 20px auto;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
            padding: 20px;
            border-radius: 10px;
        }
        .follow-list h2,
        .complaint-section h2 {
            color: #000; /* 黑色字体 */
        }

        .follow-list-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            border: 1px solid #060505;
            padding: 10px;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
        }

        .follow-list-item img {
            width: 50px;
            height: 75px;
            margin-right: 10px;
            cursor: pointer;
            border-radius: 5px;
        }

        .follow-list-item div {
            margin-right: 20px;
        }

        .complaint-section textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
            color: #000;
        }

        .complaint-section button {
            padding: 10px 20px;
            cursor: pointer;
            background-color: #007BFF;
            color: rgb(243, 240, 240);
            border: none;
            border-radius: 5px;
        }

        nav ul {
            list-style-type: none;
            padding: 0;
        }

        nav ul li {
            display: inline;
            margin-right: 10px;
        }

        nav ul li a {
            color: #efe3e3;
            text-decoration: none;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        nav ul li a:hover {
            background-color: rgba(0, 0, 0, 0.5);
        }

        .search-bar {
            display: flex;
            align-items: center;
        }

        .search-bar input {
            border-radius: 5px 0 0 5px;
            border: none;
            padding: 10px;
        }

        .search-bar button {
            border-radius: 0 5px 5px 0;
            border: none;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
        }
    </style>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.css">
</head>

<body>
    <!-- 导航栏 -->
    <nav>
        <ul>
            <li>
                <a href="dashboard.html"> <i class="fas fa-home"></i> 返回</a>
            </li>
            <li class="search-bar">
                <input type="text" id="searchInput" placeholder="搜索...">
                <button onclick="search()"><i class="iconfont iconchazhao"></i></button>
            </li>
            <li><a href="https://www.taobao.com" target="_blank">编者推荐</a></li>
            <li><a href="https://yiyan.baidu.com/" target="_blank">AI导航</a></li>
        </ul>
    </nav>

    <div class="header">
        <h1>我的主页</h1>
    </div>
    <div class="follow-list">
        <h2>关注列表</h2>
        <div id="followListContainer">
            <!-- 关注列表内容 -->
        </div>
    </div>
    <div class="complaint-section">
        <h2>申诉</h2>
        <textarea id="complaintText" placeholder="输入您的申诉理由..."></textarea>
        <button onclick="submitComplaint()">提交申诉</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const userId = localStorage.getItem('user_id');
        const identity = localStorage.getItem('identity');
        const token = localStorage.getItem('access_token');

        function goToMain() {
            window.location.href = 'dashboard.html';
        }

        async function fetchFollowList() {
            try {
                const response = await fetch('http://192.168.78.248:5000/user_love', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${token}`
                    },
                    body: JSON.stringify({ user_id: parseInt(userId) })
                });
                if (response.ok) {
                    const followList = await response.json();
                    displayFollowList(followList.films);
                } else {
                    console.error('Failed to fetch follow list');
                }
            } catch (error) {
                console.error('Error:', error);
            }
        }

        function displayFollowList(films) {
            const container = document.getElementById('followListContainer');
            container.innerHTML = ''; // 清空当前内容
            films.forEach(item => {
                const div = document.createElement('div');
                div.className = 'follow-list-item';
                div.innerHTML = `
                    <img src="${item.www_http}" alt="${item.film_name}" onclick="goToMovieDetail(${item.film_id})">
                    <div>
                        <div class="film-name" onclick="goToMovieDetail(${item.film_id})" style="cursor: pointer; color: blue;">${item.film_name}</div>
                    </div>
                `;
                container.appendChild(div);
            });
        }

        function goToMovieDetail(filmId) {
            localStorage.setItem('selectedFilmId', filmId);
            window.location.href = 'movie.html';
        }

        async function submitComplaint() {
            const complaintText = document.getElementById('complaintText').value.trim();
            if (complaintText === '') {
                Swal.fire('错误', '请填写申诉理由', 'error');
                return;
            }
            try {
                const response = await fetch('http://192.168.78.248:5000/user_complain', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${token}`
                    },
                    body: JSON.stringify({ user_id: parseInt(userId), complain: complaintText })
                });
                if (response.ok) {
                    Swal.fire('成功', '申诉成功！', 'success');
                    document.getElementById('complaintText').value = '';
                } else {
                    console.error('Failed to submit complaint');
                    Swal.fire('错误', '申诉失败，请重试', 'error');
                }
            } catch (error) {
                console.error('Error:', error);
                Swal.fire('错误', '申诉失败，请重试', 'error');
            }
        }

        window.onload = function () {
            fetchFollowList();
        }
        
        function search() {
            const searchInput = document.getElementById('searchInput').value;
            const userId = localStorage.getItem('user_id') || 'guest';
            const token = localStorage.getItem('access_token');

            if (searchInput.trim() === '') {
                alert('请输入搜索内容');
                return;
            }

            const requestData = {
                user_id: parseInt(userId),
                search: searchInput
            };

            fetch('http://192.168.78.248:5000/search', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                },
                body: JSON.stringify(requestData)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'successfully') {
                        localStorage.setItem('searchResults', JSON.stringify(data.film));
                        window.location.href = 'search.html';
                    } else {
                        alert('暂无搜索结果');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
    </script>
</body>

</html>
